const {
    Keypair,
    Transaction,
    SystemProgram,
    LAMPORTS_PER_SOL,
    TransactionMessage,
    TransactionInstruction
} = require("@solana/web3.js");

const {
    getKeyPairUser1, getKeyPairUser2
} = require('./MyKeypair');

const config = require('./config');


const solChain = require('./SolChain');

const {transactionMessage }= require('./utils');

const run = async ()=>{

    let logging_program_keypair = require('./program_keypair/spl_example_logging-keypair');
    let secretKey = Uint8Array.from(logging_program_keypair);
    let programKeypair = Keypair.fromSecretKey(secretKey);

    let programID = programKeypair.publicKey;

    let newAccount = Keypair.generate();
    const fromKeypair = await getKeyPairUser1();
    console.log('newAccount.publicKey', newAccount.publicKey.toBase58());
    let instruction = new TransactionInstruction({

        keys:[{pubkey:fromKeypair.publicKey, isSigner:false, isWritable:true}],
        programId:programID,
        data:Buffer.from("haolifeng--haohao")

    });
    let latestBlockhash = await solChain.getLatestBlockhash();

    let payerPk = fromKeypair.publicKey;

    let tx = transactionMessage(payerPk, latestBlockhash.blockhash, [instruction]);
    console.log('tx: ', tx);


    let balance = await solChain.getBalance(fromKeypair.publicKey);
    console.log('from balance: ', balance);
    await tx.sign([fromKeypair]);
    let rawTx = tx.serialize();


    let ret = await solChain.sendRawTransaction(rawTx);
    console.log('ret: ', ret);
    //5LdMpjL2iWwpUUP1RmvwBz37wwqzQhLE43GVaFzRjyVg9332reiywtUj5hxRCD9cifBFbQebyoAoLdx5DFNZadKV
}

const getTx = async ()=> {
    let tx = await solChain.getTransaction("5LdMpjL2iWwpUUP1RmvwBz37wwqzQhLE43GVaFzRjyVg9332reiywtUj5hxRCD9cifBFbQebyoAoLdx5DFNZadKV");
    console.log('tx: ', tx)
}
//run();
getTx();